#include "MessageHandler.h"

#include <iostream>

MessageHandler::MessageHandler():Thread("MessageHandler") {
    this->inputWebQueue = std::make_unique<ShareQueue<std::shared_ptr<webDataInfo>>>();
    this->httpIoc = std::make_shared<boost::asio::io_context>();

}

void MessageHandler::add_message_to_queue(std::shared_ptr<webDataInfo> webData) {
    std::cout << "Put the web info into queue" << std::endl;
    this->inputWebQueue->put(webData);
}

void MessageHandler::msgProcess(const nlohmann::json& msgJson){
    if(msgJson.contains("msg")) {
        auto result = msgJson["msg"];
        std::cout << "result:" << result << std::endl;
    }else
        std::cout << "msg not found!"  << std::endl;
}

void MessageHandler::process() {
    static std::atomic<int> count = 0;
    try {
        if (this->inputWebQueue->hasData()) {
            auto inputWebInfo = this->inputWebQueue->get(true);
            //std::cout << "msg[" << count++ << "]:" << inputWebInfo->content << std::endl;
            nlohmann::json msgJson = nlohmann::json::parse(inputWebInfo->content);
            std::cout << "msgJson:" << msgJson << std::endl;
            this->msgProcess(msgJson);
        }

    }catch (std::exception& e) {
        std::cerr << "Message handler error: " << e.what() << std::endl;
    }
}

